package bo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class BookDao implements IBookDao {
	protected static String add_sql="insert into namecard(id,name,sex,username,userpwd,mail) values(?,?,?,?,?,?)";
	protected static String search_sql="select * from namecard where username = ? and userpwd = ?";
	protected static String delete_sql="delete from namecard where id=?";
	protected static String add_bin_sql="insert into bin(id,name,sex,username,userpwd,mail) values(?,?,?,?,?,?)";
	protected static String update_sql="update namecard set id=?,name=?,sex=?,username=?,userpwd=?,mail=? where id=?";
	
	@Override
	public book add(book b) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(add_sql);
		pstmt.setString(1,b.getId());
		pstmt.setString(2,b.getName());
		pstmt.setString(3,b.getSex());
		pstmt.setString(4,b.getUsername());
		pstmt.setString(5,b.getUserpwd());
		pstmt.setString(6,b.getMail());
		pstmt.executeUpdate();
		DBConnection.free(conn, pstmt, null);
		return b;
		
	}

	@Override
	public void delete(book b) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(delete_sql);
		PreparedStatement pstmtt=conn.prepareStatement(add_bin_sql);
		pstmtt.setString(1,b.getId());
		pstmtt.setString(2,b.getName());
		pstmtt.setString(3,b.getSex());
		pstmtt.setString(4,b.getUsername());
		pstmtt.setString(5,b.getUserpwd());
		pstmtt.setString(6,b.getMail());
		pstmtt.executeUpdate();
		pstmt.setString(1,b.getId());
		pstmt.executeUpdate();
		DBConnection.free(conn, pstmt, null);
		DBConnection.free(conn, pstmtt, null);
	}

	@Override
	public List<book> search(book b) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(search_sql);
		pstmt.setString(1, b.getUsername());
		pstmt.setString(2, b.getUserpwd());
		ResultSet rs=pstmt.executeQuery();
		List<book> bb=new ArrayList<book>();
		while(rs.next()) {
			book b1=new book();
			b1.setId(rs.getString(1));
			b1.setName(rs.getString(2));
			b1.setSex(rs.getString(3));	
			b1.setUsername(rs.getString(4));
			b1.setUserpwd(rs.getString(5));
			b1.setMail(rs.getString(6));
			bb.add(b1);
		}
		DBConnection.free(conn, pstmt, rs);
		return bb;
		
	}
	

	@Override
	public void update(book b) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(update_sql);
		pstmt.setString(1, b.getId());
		pstmt.setString(2,b.getName());
		pstmt.setString(3, b.getSex());
		pstmt.setString(4, b.getUsername());
		pstmt.setString(5,b.getUserpwd());
		pstmt.setString(6,b.getMail());
		pstmt.setString(7, b.getId());
		int rowCount=pstmt.executeUpdate();
		if(rowCount==0) {
			throw new Exception("Update Error,card id is"+b.getId());
		}
		DBConnection.free(conn, pstmt, null);

	}
	

}
